package org.example.servletpractice.servlet;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.text.SimpleDateFormat;
import java.util.Date;

@WebServlet("/refreshServlet")
public class RefreshServlet extends HttpServlet {

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //设置每隔1秒刷新
        resp.setIntHeader("Refresh",1);
        // 设置响应内容类型
        resp.setContentType("text/html;charset=UTF-8");
        //获取当前时间
        Date date = new Date();
        //格式化时间
        SimpleDateFormat ft =
                new SimpleDateFormat("yyyy.MM.dd  hh:mm:ss E a ");

        PrintWriter writer = resp.getWriter();
        writer.println("<h1>当前时间: "+ft.format(date)+"</h1>");
        writer.flush();
    }
}
